package com.dragon.mmochat.util;

import java.util.ArrayList;
import java.util.List;

import com.dragon.mmochat.MmochatConstant;
import com.skymobi.buffer.SkymobiBuffer;
import com.skymobi.message.widget.ItemEvent;
import com.skymobi.message.widget.TLVStructure;
import com.skymobi.message.widget.WinType;
import com.skymobi.util.Text;

//带按键事件的组合框控件
/*示例
 CreateKeyEventTlvWin tlv = new CreateKeyEventTlvWin();
 tlv.setWinType(WinType.List);
 tlv.setLeftName("确定");
 tlv.setRightName("返回");
 tlv.setTitle("test");
 tlv.setWinHeight(230);
 tlv.addRightTLV(new CloseWinTLV());

 TlvKeyEvent keyEvent = new TlvKeyEvent();
 keyEvent.setKeyValue(TlvKeyEvent.MR_KEY_0);
 keyEvent.addKeyEvent(new ClearDataTLV());
 keyEvent.addKeyEvent(new CloseWinTLV());

 tlv.addKeyEventTLV(keyEvent);

 keyEvent = new TlvKeyEvent();
 keyEvent.setKeyValue(TlvKeyEvent.MR_KEY_1);
 keyEvent.addKeyEvent(new CloseWinTLV());

 tlv.addKeyEventTLV(keyEvent);
 */
public class CreateKeyEventTlvWin extends TLVStructure {
	private int winId;
	private WinType winType = WinType.List;
	private int time;
	private String leftName = "";
	private String rightName = "";
	private String title = "";
	private Text content = new Text("");
	private List<TLVStructure> leftTLVs;
	private List<TLVStructure> rightTLVs;
	private List<TlvKeyEvent> keyEventTLVs;
	private List<ItemEvent> itemEvents;

	public void setWinId(int winId) {
		this.winId = winId;
	}

	public void setWinType(WinType winType) {
		this.winType = winType;
	}

	public void setTime(int time) {
		this.time = time;
	}

	public void setWinHeight(int height) {
		if (height > 255) {
			height = 255;
		}
		time = height;
	}

	public void setLeftName(String leftName) {
		this.leftName = leftName;
	}

	public void setRightName(String rightName) {
		this.rightName = rightName;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setContent(Text content) {
		this.content = content;
	}

	public void setLeftTLVs(List<TLVStructure> leftTLVs) {
		this.leftTLVs = leftTLVs;
	}

	public void setRightTLVs(List<TLVStructure> rightTLVs) {
		this.rightTLVs = rightTLVs;
	}

	public void setItemEvents(List<ItemEvent> itemEvents) {
		this.itemEvents = itemEvents;
	}

	public void setKeyEventTLVs(List<TlvKeyEvent> keyEventTLVs) {
		this.keyEventTLVs = keyEventTLVs;
	}

	public void addLeftTLV(TLVStructure tlv) {
		if (leftTLVs == null) {
			synchronized (this) {
				if (leftTLVs == null)
					leftTLVs = new ArrayList<TLVStructure>();
			}
		}
		leftTLVs.add(tlv);
	}

	public void addRightTLV(TLVStructure tlv) {
		if (rightTLVs == null) {
			synchronized (this) {
				if (rightTLVs == null)
					rightTLVs = new ArrayList<TLVStructure>();
			}
		}
		rightTLVs.add(tlv);
	}

	public void addKeyEventTLV(TlvKeyEvent tlv) {
		if (keyEventTLVs == null) {
			synchronized (this) {
				if (keyEventTLVs == null)
					keyEventTLVs = new ArrayList<TlvKeyEvent>();
			}
		}
		keyEventTLVs.add(tlv);
	}

	public void addItemEvent(ItemEvent event) {
		if (itemEvents == null) {
			synchronized (this) {
				if (itemEvents == null)
					itemEvents = new ArrayList<ItemEvent>();
			}
		}
		itemEvents.add(event);
	}

	/**
	 * 添加列表项
	 * 
	 * @param itemName
	 *            项名称
	 * @param tlvs
	 *            事件控制命令
	 */
	public void addItemEvent(String itemName, TLVStructure... tlvs) {
		ItemEvent event = new ItemEvent();
		event.setName(itemName);
		for (TLVStructure tlv : tlvs)
			event.add(tlv);
		addItemEvent(event);
	}

	/**
	 * 添加列表项
	 * 
	 * @param itemName
	 *            项名称
	 * @param tlvs
	 *            事件控制命令
	 */
	public void addItemEvent(String itemName, List<TLVStructure> tlvs) {
		ItemEvent event = new ItemEvent();
		event.setName(itemName);
		for (TLVStructure tlv : tlvs)
			event.add(tlv);
		addItemEvent(event);
	}

	/**
	 * 添加列表项
	 * 
	 * @param picId
	 *            图片号
	 * @param itemName
	 *            项名称
	 * @param tlvs
	 *            事件控制命令
	 */
	public void addItemEvent(int picId, String itemName, TLVStructure... tlvs) {
		ItemEvent event = new ItemEvent();
		event.setName(itemName);
		event.setPicId(picId);
		for (TLVStructure tlv : tlvs)
			event.add(tlv);
		addItemEvent(event);
	}

	/**
	 * 添加列表项
	 * 
	 * @param itemName
	 *            项名称
	 */
	public void addItemEvent(String itemName) {
		ItemEvent event = new ItemEvent();
		event.setName(itemName);
		addItemEvent(event);
	}

	@Override
	public int getTag() {
		return MmochatConstant.showWinWithKeyEventTag;
	}

	@Override
	public void encodeTLV(SkymobiBuffer out) {
		out.putUnsignedShort(winId);
		out.putUnsigned((short) winType.value());
		out.putUnsigned((short) time);
		out.putFixupLengthString(leftName, 4);
		out.putFixupLengthString(rightName, 4);
		out.putPrefixedString(title, 2);
		content.encode(out);
		out.putUnsignedShort(0);
		if (leftTLVs != null) {
			int start = out.position();
			for (TLVStructure structure : leftTLVs)
				structure.encode(out);
			out.putUnsignedShort(start - 2, out.position() - start);
		}
		out.putUnsignedShort(0);
		if (rightTLVs != null) {
			int start = out.position();
			for (TLVStructure structure : rightTLVs)
				structure.encode(out);
			out.putUnsignedShort(start - 2, out.position() - start);
		}
		out.putUnsignedShort(0);
		if (keyEventTLVs != null) {
			int start = out.position();
			for (TlvKeyEvent structure : keyEventTLVs)
				structure.encode(out);
			out.putUnsignedShort(start - 2, out.position() - start);
		}
		if (itemEvents != null) {
			out.putUnsigned((short) itemEvents.size());
			for (ItemEvent event : itemEvents)
				event.encode(out);
		} else {
			out.putUnsigned((short) 0);
		}
	}
}
